package com.example.huanglinqing.mqtt;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.huanglinqing.service.MQTTService;

public class Main2Activity extends AppCompatActivity {
    private IntentFilter intentFilter;
    private  NetworkChangeReceiver networkChangeReceiver;
private Button btn_bangding,btn_jiechu;
private  MyConn conn;
private MQTTService.MyBinder myBinder;
public static String[]par=new String[]{"111","222"};

    private class MyConn implements ServiceConnection{

        //当服务连接成功调用
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //获取中间人对象
            myBinder = (MQTTService.MyBinder) service;
        }
        //失去连接
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }}
    @Override
    protected void onDestroy() {
        //当activity 销毁的时候 解绑服务
        unbindService(conn);
        super.onDestroy();
    }






    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        btn_bangding=findViewById(R.id.bangding);
        btn_jiechu=findViewById(R.id.jiechu);
        intentFilter=new IntentFilter();
        intentFilter.addAction("android.net");
        networkChangeReceiver=new  NetworkChangeReceiver();
        registerReceiver(networkChangeReceiver,intentFilter);
        Intent intent=new Intent(Main2Activity.this,MQTTService.class);
        startService(intent);
        Intent bindIntent=new Intent(Main2Activity.this,MQTTService.class);
        conn=new MyConn();
        bindService(bindIntent,conn,BIND_AUTO_CREATE);


        btn_bangding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                myBinder.callBanZheng(10000000);
            }


        });
        btn_jiechu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


            }
        });




    //    cc();
    }

    class  NetworkChangeReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {

            String data=intent.getStringExtra("zhi1");
         String a="ceshiyuju";

            if (MQTTService.str1.equals("33")){
          Toast.makeText(Main2Activity.this,"33",Toast.LENGTH_SHORT).show();
      }else if (MQTTService.str1.equals("44")){
          Toast.makeText(Main2Activity.this,"44",Toast.LENGTH_SHORT).show();
      }


//if (data.equals("ceshiyuju") ){
//    Toast.makeText(Main2Activity.this,MQTTService.str1,Toast.LENGTH_SHORT).show();
//
//}

//
//            if (MQTTService.str1.equals("33")){
//           Toast.makeText(Main2Activity.this,"33",Toast.LENGTH_SHORT).show();
//       }else if (MQTTService.str1.equals("44")){
//           Toast.makeText(Main2Activity.this,"44",Toast.LENGTH_SHORT).show();
//       }
//
        }
    }


//    public void cc() {
////       if (MQTTService.str1.equals("33")){
////           Toast.makeText(Main2Activity.this,"33",Toast.LENGTH_SHORT).show();
////       }else if (MQTTService.str1.equals("44")){
////           Toast.makeText(Main2Activity.this,"44",Toast.LENGTH_SHORT).show();
////       }
//        if (!MQTTService.str1.isEmpty()) {
//            Toast.makeText(Main2Activity.this, str1, Toast.LENGTH_SHORT).show();
//        }


  //  }

}
